<?php
/**
 * Teacher data mapper
 *
 * Implements the Data Mapper design pattern
 *
 * @version $Id: TeacherMapper.php 73 2010-04-25 00:30:44Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Teacher_Model_DbTable_Teacher
 * @package teacher
 * @subpackage Model
 */
class Teacher_Model_TeacherMapper
{

    protected $_dbTable = null;

    /**
     * Specify Zend_Db_Table instance to use for data operations
     *
     * @param Zend_Db_Table_Abstract $dbTable
     * @return Teacher_Model_TeacherMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
        	$dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
        	throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Teacher_Model_Teacher if no instance registered
     *
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
        $this->setDbTable('Teacher_Model_DbTable_Teacher');
        }
        return $this->_dbTable;
    }

    /**
     * Find a teacher entry by a key
     *
     * @param int id
     * @param Teacher_Model_Teacher teacher
     * @return void
     */
    public function save($teacher)
    {
        $data = array(
        	'sn' => $teacher->getSn(),
        	'identification_number' => $teacher->getIdentification_number(),
        	'english_name' => $teacher->getEnglish_name(),
        	'sex' => $teacher->getSex(),
        	'birthday' => $teacher->getBirthday(),
        	'born_place' => $teacher->getBorn_place(),
        	'marriaged' => $teacher->getMarriaged(),
        	'address' => $teacher->getAddress(),
        	'phone_home' => $teacher->getPhone_home(),
        	'phone_office' => $teacher->getPhone_office(),
        	'phone_mobile' => $teacher->getPhone_mobile(),
        	'website' => $teacher->getWebsite(),
        	'education_level' => $teacher->getEducation_level(),
        	'overseas' => $teacher->getOverseas(),
        	'certification_type' => $teacher->getCertification_type(),
        	'certification_number' => $teacher->getCertification_number(),
        	'certification_subject' => $teacher->getCertification_subject(),
        	'disability' => $teacher->getDisability(),
        	'appoint_date' => $teacher->getAppoint_date(),
        	'arrive_date' => $teacher->getArrive_date(),
        	'approve_date' => $teacher->getApprove_date(),
        	'approve_number' => $teacher->getApprove_number(),
        	'post_number' => $teacher->getPost_number(),
        	'bywork_number' => $teacher->getBywork_number(),
        	'condition_state' => $teacher->getCondition_state(),
        	'current_kind_sn' => $teacher->getCurrent_kind_sn(),
        	'current_title_sn' => $teacher->getCurrent_title_sn(),
        	'current_office_id' => $teacher->getCurrent_office_id(),
        	'memo' => $teacher->getMemo(),
        	'photo_name' => $teacher->getPhoto_name(),
        	'update_time' => $teacher->getUpdate_time(),
        	'update_sn' => $teacher->getUpdate_sn(),
        );
        if (null === ($id = $teacher->getId())) {
        	unset($data['id']);
        	$this->getDbTable()->insert($data);
        } else {
        	$this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    /**
     * Save a teacher entry
     *
     * @param Teacher_Model_Teacher teacher
     * @return void
     */
    public function find($id, $teacher)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
        	return;
        }
        $row = $result->current();
        $teacher->setUsername($row->username)
        	->setSn($row->sn)
        	->setIdentification_number($row->identification_number)
        	->setEnglish_name($row->english_name)
        	->setSex($row->sex)
        	->setBirthday($row->birthday)
        	->setBorn_place($row->born_place)
        	->setMarriaged($row->marriaged)
        	->setAddress($row->address)
        	->setPhone_home($row->phone_home)
        	->setPhone_office($row->phone_office)
        	->setPhone_mobile($row->phone_mobile)
        	->setWebsite($row->website)
        	->setEducation_level($row->education_level)
        	->setOverseas($row->overseas)
        	->setCertification_type($row->certification_type)
        	->setCertification_number($row->certification_number)
        	->setCertification_subject($row->certification_subject)
        	->setDisability($row->disability)
        	->setAppoint_date($row->appoint_date)
        	->setArrive_date($row->arrive_date)
        	->setApprove_date($row->approve_date)
        	->setApprove_number($row->approve_number)
        	->setPost_number($row->post_number)
        	->setBywork_number($row->bywork_number)
        	->setCondition_state($row->condition_state)
        	->setCurrent_kind_sn($row->current_kind_sn)
        	->setCurrent_title_sn($row->current_title_sn)
        	->setCurrent_office_id($row->current_office_id)
        	->setMemo($row->memo)
        	->setPhoto_name($row->photo_name)
        	->setUpdate_time($row->update_time)
        	->setUpdate_sn($row->update_sn)
        	;
    }

    /**
     * Fetch all teacher entries
     *
     * @return array
     */
    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
        	$entry = new Teacher_Model_Teacher();
        	$entry->setUsername($row->username)
        	->setSn($row->sn)
        	->setIdentification_number($row->identification_number)
        	->setEnglish_name($row->english_name)
        	->setSex($row->sex)
        	->setBirthday($row->birthday)
        	->setBorn_place($row->born_place)
        	->setMarriaged($row->marriaged)
        	->setAddress($row->address)
        	->setPhone_home($row->phone_home)
        	->setPhone_office($row->phone_office)
        	->setPhone_mobile($row->phone_mobile)
        	->setWebsite($row->website)
        	->setEducation_level($row->education_level)
        	->setOverseas($row->overseas)
        	->setCertification_type($row->certification_type)
        	->setCertification_number($row->certification_number)
        	->setCertification_subject($row->certification_subject)
        	->setDisability($row->disability)
        	->setAppoint_date($row->appoint_date)
        	->setArrive_date($row->arrive_date)
        	->setApprove_date($row->approve_date)
        	->setApprove_number($row->approve_number)
        	->setPost_number($row->post_number)
        	->setBywork_number($row->bywork_number)
        	->setCondition_state($row->condition_state)
        	->setCurrent_kind_sn($row->current_kind_sn)
        	->setCurrent_title_sn($row->current_title_sn)
        	->setCurrent_office_id($row->current_office_id)
        	->setMemo($row->memo)
        	->setPhoto_name($row->photo_name)
        	->setUpdate_time($row->update_time)
        	->setUpdate_sn($row->update_sn)
        	;
        	$entries[] = $entry;
        }
        return $entries;
    }


}
